
import axios from 'axios';
import {WEB_URL}  from './Config'
//const WEB_URL = 'http://192.168.0.102:9003/drying';    //全局URL
//const WEB_URL = 'http://120.79.217.84:9006/drying';    //服务器地址


//封装GET查询字段请求
// str 接口
// fields 想要得到的字段，数组形式，例：["id","status","type","code"]
const GETFIELDS = (str,fields) =>{
    return new Promise((resolve,reject)=>{
        //Array.isArray(fields) ? reject('fields不是一个数组！'):''
        let includeFileds = '';    
        fields.map(item=>{
            return includeFileds += 'includeFields='+item+'&';
        })
        let url = WEB_URL + str +'?'+includeFileds.substring(0,includeFileds.length-1)
        axios
            .get(url)
            .then(res=>{
                resolve(res.data)
            }).catch(error=>{
                reject(error)
            })
        })
}
// 封装GET请求
// str 接口
// param 参数 对象形式 例：{"name":"haroro","id":"123"}
const GET = (str,param) =>{
    return new Promise((resolve,reject)=>{ 
        let url = WEB_URL + str;
       console.log(url)        
        axios
            .get(url,param)
            .then(res=>{
                if(res){
                    resolve(res.data)
                }else{
                    resolve({msg:"网络错误，稍后再试",code:"500",success:"false"})
                }
                
            }).catch(error=>{
                reject(error)
            })
        })
}
// 封装POST请求
// str 接口
// param 参数 对象形式
const POST = (str,param)=>{
    let url = WEB_URL + str;
    return new Promise((resolve,reject)=>{
        axios
            .post(url,param)
            .then(res=>{
                resolve(res.data)
            }).catch(error=>{
                reject(error)
            })
    })
}
// 封装PUT请求
// str 接口
// param 参数 对象形式
const PUT = (str,param)=>{
    let url = WEB_URL + str;
    return new Promise((resolve,reject)=>{
        axios
            .put(url,param)
            .then(res=>{
                resolve(res.data)
            }).catch(error=>{
                reject(error)
            })
    })
}
// 封装DELETE请求
// str 接口
// param 参数 对象形式
const DELETE = (str,param)=>{
    let url = WEB_URL + str;
    return new Promise((resolve,reject)=>{
        axios
            .delete(url,param)
            .then(res=>{
                resolve(res.data)
            }).catch(error=>{
                reject(error)
            })
    })
}
// 封装table列表接口
const GETTABLEROWS = (str,param)=>{
    let url = WEB_URL + url;
    return new Promise((resolve,reject)=>{
        axios
            .get(url,param)
            .then(res=>{
                
            })
    })
}






export { WEB_URL, GETFIELDS, GET, POST, PUT, DELETE}